package cn.iocoder.yudao.module.biz.controller.admin.safetyCheck;


import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.infra.dal.dataobject.oss.OssDO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.biz.controller.admin.safetyCheck.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.safetyCheck.BizSafetyCheckDO;
import cn.iocoder.yudao.module.biz.convert.safetyCheck.BizSafetyCheckConvert;
import cn.iocoder.yudao.module.biz.service.safetyCheck.BizSafetyCheckService;

@Api(tags = "管理后台 - 安全检查")
@RestController
@RequestMapping("/biz/safety-check")
@Validated
public class BizSafetyCheckController {

    @Resource
    private BizSafetyCheckService safetyCheckService;

    @PostMapping("/create")
    @ApiOperation("创建安全检查")
    @PreAuthorize("@ss.hasPermission('biz:safety-check:create')")
    public CommonResult<String> createSafetyCheck(@Valid @RequestBody BizSafetyCheckCreateReqVO createReqVO) {
        return success(safetyCheckService.createSafetyCheck(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新安全检查")
    @PreAuthorize("@ss.hasPermission('biz:safety-check:update')")
    public CommonResult<Boolean> updateSafetyCheck(@Valid @RequestBody BizSafetyCheckUpdateReqVO updateReqVO) {
        safetyCheckService.updateSafetyCheck(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除安全检查")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:safety-check:delete')")
    public CommonResult<Boolean> deleteSafetyCheck(@RequestParam("id") String id) {
        safetyCheckService.deleteSafetyCheck(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得安全检查")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:safety-check:query')")
    public CommonResult<BizSafetyCheckPageJoinVO> getSafetyCheck(@RequestParam("id") String id) {
        BizSafetyCheckPageReqVO vo=new BizSafetyCheckPageReqVO();
        vo.setPageNo(0);
        vo.setPageSize(10);
        vo.setId(id);
        List<BizSafetyCheckPageJoinVO> list = safetyCheckService.selectSafetyCheck(vo);
        BizSafetyCheckPageJoinVO joinVO=list.get(0);

        if(!ObjectUtils.isEmpty(joinVO.getCheckFileId())){
            OssDO checkFileOss=new OssDO();
            checkFileOss.setOriginalName(joinVO.getCheckFileName());
            checkFileOss.setUrl(joinVO.getCheckFileUrl());
            checkFileOss.setId(Long.parseLong(joinVO.getCheckFileId()));
            List<OssDO> ossDOList = new ArrayList<>();
            ossDOList.add(checkFileOss);
            joinVO.setCheckFileOss(ossDOList);
        }

        if(!ObjectUtils.isEmpty(joinVO.getRectificationFileId())) {
            OssDO rectificationFileOss = new OssDO();
            rectificationFileOss.setOriginalName(joinVO.getRectificationFileName());
            rectificationFileOss.setUrl(joinVO.getRectificationFileUrl());
            if (!ObjectUtils.isEmpty(joinVO.getRectificationFileId())) {
                rectificationFileOss.setId(Long.parseLong(joinVO.getRectificationFileId()));
            }
            List<OssDO> rectificationFileOssList = new ArrayList<>();
            rectificationFileOssList.add(rectificationFileOss);
            joinVO.setRectificationFileOss(rectificationFileOssList);
        }

        if(!ObjectUtils.isEmpty(joinVO.getRectificationImageId())) {
            OssDO rectificationImageOss = new OssDO();
            rectificationImageOss.setOriginalName(joinVO.getRectificationImageName());
            rectificationImageOss.setUrl(joinVO.getRectificationImageUrl());
            if (!ObjectUtils.isEmpty(joinVO.getRectificationImageId())) {
                rectificationImageOss.setId(Long.parseLong(joinVO.getRectificationImageId()));
            }
            List<OssDO> rectificationImageOssList = new ArrayList<>();
            rectificationImageOssList.add(rectificationImageOss);
            joinVO.setRectificationImageOss(rectificationImageOssList);
        }
        return success(joinVO);
    }

    @GetMapping("/list")
    @ApiOperation("获得安全检查列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('biz:safety-check:query')")
    public CommonResult<List<BizSafetyCheckRespVO>> getSafetyCheckList(@RequestParam("ids") Collection<String> ids) {
        List<BizSafetyCheckDO> list = safetyCheckService.getSafetyCheckList(ids);
        return success(BizSafetyCheckConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得安全检查分页")
    @PreAuthorize("@ss.hasPermission('biz:safety-check:query')")
    public CommonResult<PageResult<BizSafetyCheckRespVO>> getSafetyCheckPage(@Valid BizSafetyCheckPageReqVO pageVO) {
        PageResult<BizSafetyCheckDO> pageResult = safetyCheckService.getSafetyCheckPage(pageVO);
        return success(BizSafetyCheckConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping(value = "/selectSafetyCheck")
    public PageResult<BizSafetyCheckPageJoinVO> selectSafetyCheck(BizSafetyCheckPageReqVO pageVO) {
        PageResult<BizSafetyCheckPageJoinVO> result=new PageResult<>();
        Integer pageNo=pageVO.getPageNo();
        Integer pageSize=  pageVO.getPageSize();
        //设置为limit开始的条数
        pageVO.setPageNo((pageNo-1)*pageSize);
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        pageVO.setDeptId(loginUser.getDeptId()+"");
        List<BizSafetyCheckPageJoinVO> list = safetyCheckService.selectSafetyCheck(pageVO);
        Integer count= safetyCheckService.selectSafetyCheckCount(pageVO);
        result.setList(list);
        result.setTotal((long) count);
        return result;
    }


    @GetMapping("/export-excel")
    @ApiOperation("导出安全检查 Excel")
    @PreAuthorize("@ss.hasPermission('biz:safety-check:export')")
    @OperateLog(type = EXPORT)
    public void exportSafetyCheckExcel(@Valid BizSafetyCheckPageReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        //设置为limit开始的条数
        exportReqVO.setPageNo((exportReqVO.getPageNo()-1)*exportReqVO.getPageSize());
        List<BizSafetyCheckPageJoinVO> list = safetyCheckService.selectSafetyCheck(exportReqVO);
        // 导出 Excel
        List<BizSafetyCheckExcelVO> datas = BizSafetyCheckConvert.INSTANCE.convertJoin2Excel(list);
        ExcelUtils.write(response, "安全检查.xls", "数据", BizSafetyCheckExcelVO.class, datas);
    }

}
